package org.eclipse.milo.examples.server.util;

import com.typesafe.config.Config;
import com.typesafe.config.ConfigFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

public class ConfigUtil {
    public static final Logger logger = LoggerFactory.getLogger(ConfigUtil.class);

    public static String getString(Config conf, String key) {
        try {
            return conf.getString(key);
        } catch (Exception e) {
            logger.error("Read key={} failed! System exit!", key);
            logger.error(e.getMessage(), e);
            System.exit(0);
        }

        return null;
    }

    public static List<String> getStringList(Config conf, String key) {
        try {
            return conf.getStringList(key);
        } catch (Exception e) {
            logger.error("Read key={} failed! System exit!", key);
            logger.error(e.getMessage(), e);
            System.exit(0);
        }

        return null;
    }

    public static Object getAnyRef(Config conf, String key) {
        try {
            return conf.getAnyRef(key);
        } catch (Exception e) {
            logger.error("Read key={} failed! System exit!", key);
            logger.error(e.getMessage(), e);
            System.exit(0);
        }
        return null;
    }
    public static int getInt(Config conf, String key) {
        try {
            return conf.getInt(key);
        } catch (Exception e) {
            logger.error("Read key={} failed! System exit!", key);
            logger.error(e.getMessage(), e);
            System.exit(0);
        }
        return 0;
    }


    public static boolean getBoolean(Config conf, String key) {
        try {
            return conf.getBoolean(key);
        } catch (Exception e) {
            logger.error("Read key={} failed! System exit!", key);
            logger.error(e.getMessage(), e);
            System.exit(0);
        }
        return false;
    }

    public static List<Config> getConfigList(Config conf, String key) {
        try {
            return (List<Config>) conf.getConfigList(key);
        } catch (Exception e) {
            logger.error("Read key={} failed! System exit!", key);
            logger.error(e.getMessage(), e);
            System.exit(0);
        }
        return null;
    }

}

